#pragma once

#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ThreeVector.hh"
#include "G4VUserPrimaryGeneratorAction.hh"
//#include "globals.hh"

class PrimaryGeneratorMessenger;
class DetectorConstruction;

class PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
{
public:
    // The constructor defines a ParticleGun object, which allows
    // shooting a beam of particles through the experimental set-up.
    PrimaryGeneratorAction(DetectorConstruction* pDet);

    //The destructor. It deletes the ParticleGun.
    ~PrimaryGeneratorAction() override;

    //It reads the parameters of the primary particles.
    //Generates the primary event via the ParticleGun method.
    void GeneratePrimaries(G4Event* anEvent) override;

    //Get/Set methods
    void SetBeamEnergy(G4double val);

    inline void SetBeamSigmaE(G4double val) { _fSigmaE = val; };
    inline void SetBeamX(G4double val) { _fX0 = val; };
    inline void SetBeamY(G4double val) { _fY0 = val; };
    inline void SetBeamZ(G4double val) { _fZ0 = val; };
    inline void SetBeamSigmaX(G4double val) { _fSigmaX = val; };
    inline void SetBeamSigmaY(G4double val) { _fSigmaY = val; };
    inline void SetBeamSigmaZ(G4double val) { _fSigmaY = val; };
    inline void SetSigmaTheta(G4double val) { _fSigmaTheta = val; };
    inline void SetVerbose(G4int val) { _fVerbose = val; };
    inline void SetDisTypeXY(const G4String& type) { _fDisTypeXY = type; };
    inline void SetDisTypeE(const G4String& type) { _fDisTypeE = type; };
    inline void SetDisTypeTheta(const G4String& type) { _fDisTypeTheta = type; };  //2022.10.18
    inline void SetSpotDeltaY(G4double val) { _fSpotDeltaY = val; };               //2023.6.20, for QA test

    inline G4ThreeVector RotationMatrix(G4ThreeVector& locPos, G4ThreeVector& Rotx, G4ThreeVector& Roty, G4ThreeVector& Rotz) const;

    G4bool GetVerbose() const { return _fVerbose; }

private:
    void InitializeMe();

    PrimaryGeneratorAction& operator=(const PrimaryGeneratorAction& right);
    PrimaryGeneratorAction(const PrimaryGeneratorAction&);

    G4int _fCounter{};
    G4int _fVerbose;

    PrimaryGeneratorMessenger* _fMessenger;
    G4ParticleGun* _fParticleGun;
    DetectorConstruction* _fDetector;

    G4double _fX0{}, _fY0{}, _fZ0{};
    G4double _fSigmaX{}, _fSigmaY{}, _fSigmaZ{};
    G4double _fRMax2{};
    G4double _fSigmaE{};
    G4double _fSigmaTheta{};
    G4double _fEnergy{};
    G4String _fDisTypeXY{"flat"}, _fDisTypeE{"flat"};
    G4String _fDisTypeTheta{"flat"};  //2022.10.18
    G4double _fSpotDeltaY{};          //2023.6.20, for QA test

    G4ThreeVector _fPosition;
    G4ThreeVector _fDirection;

    void PrintSourceParameters();
};
